package main

// 一对一 belong to
/*
type Dog struct {
	gorm.Model
	Name   string
	GirlID uint
	Girl   Girl
}

type Girl struct {
	gorm.Model
	Name string
	//Dog  Dog
}

// 1-1 属于
func belongToSample() {

	girl := Girl{
		Model: gorm.Model{
			ID: 2,
		},
		Name: "女神2",
	}

	dog1 := Dog{
		Model: gorm.Model{
			ID: 1,
		},
		Name: "舔狗1",
		//	Girl: girl,
	}

	//GLOBAL_DB.Create(&dog1)
	GLOBAL_DB.Model(&dog1).Association("Girl").Append(&girl)

}

func blongToQuerySample() {

	//dogMap := map[string]interface{}{}

	var resultDog Dog

	GLOBAL_DB.Preload("Girl").Model(&Dog{}).First(&resultDog)
	fmt.Println(resultDog)

}


*/

/**
//have a
type Dog struct {
	gorm.Model
	Name   string
	GirlID uint
}

type Girl struct {
	gorm.Model
	Name string
	Dog  Dog
}

*
*/

//one to many 一對多

/*
type DogInfo struct {
	gorm.Model
	Money uint
	DogID uint
}

type Dog struct {
	gorm.Model
	Name   string
	GirlID uint
	Info   DogInfo
}

type Girl struct {
	gorm.Model
	Name string
	Dogs []Dog
}

*/

/*
func oneToManySample() {

	dog1 := Dog{
		Model: gorm.Model{
			ID: 1,
		},
		Name: "舔狗1",
		Info: DogInfo{
			Money: 10000,
		},
	}

	dog2 := Dog{
		Model: gorm.Model{
			ID: 2,
		},
		Name: "舔狗2",
		Info: DogInfo{
			Money: 1999,
		},
	}

	girl := Girl{
		Model: gorm.Model{
			ID: 1,
		},
		Name: "女神1",
		Dogs: []Dog{dog1, dog2},
	}

	GLOBAL_DB.Create(&girl)


}

*/

/*func oneToManyQuery() {

	var girl Girl
	//GLOBAL_DB.Preload("Dogs").First(&girl)
	//GLOBAL_DB.Preload("Dogs", 1).First(&girl)
	//GLOBAL_DB.Preload("Dogs", "name = ?", "舔狗2").First(&girl)

	GLOBAL_DB.Preload("Dogs.Info").Preload("Dogs", func(tx *gorm.DB) *gorm.DB {
		return tx.Where("name = ?", "舔狗2")
	}).First(&girl)

	GLOBAL_DB.Preload("Dogs.Info", "Money < 10000").Preload("Dogs", func(tx *gorm.DB) *gorm.DB {
		return tx.Where("name = ?", "舔狗2")
	}).First(&girl)

	GLOBAL_DB.Preload("Dogs", func(tx *gorm.DB) *gorm.DB {

		return tx.Joins("Info").Where("Money >= 10000")
	}).First(&girl)

	fmt.Println(girl)

}
*/

func CreateSomeModels() {

	/*	GLOBAL_DB.AutoMigrate(&Dog{})
		GLOBAL_DB.AutoMigrate(&Girl{})
		GLOBAL_DB.AutoMigrate(&DogInfo{})*/

	//belongToSample()

	//GLOBAL_DB.AutoMigrate(&Girl{})

	//blongToQuerySample()

	//oneToManySample()

	//oneToManyQuery()
	//
}
